package de.tum.icuproject;

import java.util.Locale;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphView.LegendAlign;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;
import com.jjoe64.graphview.GraphViewStyle;
import com.jjoe64.graphview.LineGraphView;

import de.tum.icuproject.utils.AppUtils;

/**
 * The MonitorFragment contains the current values sent by the sensors
 * visualized both in a graphical (within a real-time GraphView object) and
 * numerical way.
 * 
 * @author Daniele Volpi
 * 
 */
public class MonitorFragment extends Fragment {
	
	private static GraphViewSeries heartRateSeries;
	private static TextView heartRateTextView;
	private static GraphViewSeries respirationSeries;
	private static TextView respirationTextView;
	private static GraphViewSeries speedSeries;
	private static TextView speedTextView;
	private static GraphViewSeries loadSeries;
	private static TextView loadTextView;

	private static double lastXValue = 1d;
	
	private static GraphView graphViewHRAndResp;
	private static GraphView graphViewSpeedAndLoad;

	public static void updateGraphViews(double heartRate, double resp, double speed,
			double load) {
		lastXValue += 1d;
				
		heartRateSeries.appendData(new GraphViewData(lastXValue, heartRate),
				false);
		String value = String.format(Locale.US, "%3d", Math.round(heartRate));
		heartRateTextView.setText(value);

		respirationSeries.appendData(new GraphViewData(lastXValue, resp), true);
		value = String.format(Locale.US, "%3d", Math.round(resp));
		respirationTextView.setText(value);
		
		speedSeries.appendData(new GraphViewData(lastXValue, speed), false);
		value = String.format(Locale.US, "%3d", Math.round(speed));
		speedTextView.setText(value);
		
		loadSeries.appendData(new GraphViewData(lastXValue, load), true);
		value = String.format(Locale.US, "%3d", Math.round(load));
		loadTextView.setText(value);

		// Repaint the GraphViews.
		graphViewHRAndResp.getChildAt(1).invalidate();
		graphViewSpeedAndLoad.getChildAt(1).invalidate();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_monitor, container,
				false);
		// Create the GraphView HeartRate and Respiration object and set its
		// parameters.
		graphViewHRAndResp = new LineGraphView(getActivity(), "");
		graphViewHRAndResp.setViewPort(0, 24);
		graphViewHRAndResp.setGraphViewStyle(new GraphViewStyle(Color.BLACK,
				Color.BLACK, Color.LTGRAY));
		graphViewHRAndResp.setScrollable(true);
		graphViewHRAndResp.setShowLegend(true);
		graphViewHRAndResp.setLegendAlign(LegendAlign.BOTTOM);

		// Create the different GraphViewSeries and the respective styles for
		// the first GraphView.
		GraphViewSeriesStyle heartRateSeriesStyle = new GraphViewSeriesStyle(
				AppUtils.HR_COLOR, 4);
		heartRateSeries = new GraphViewSeries(AppUtils.HEART_RATE,
				heartRateSeriesStyle, new GraphViewData[0]);
		GraphViewSeriesStyle respirationSeriesStyle = new GraphViewSeriesStyle(
				AppUtils.RESPIRATION_COLOR, 4);
		respirationSeries = new GraphViewSeries(AppUtils.RESPIRATION,
				respirationSeriesStyle, new GraphViewData[0]);

		// Create the GraphView Speed and Load object and set its parameters.
		graphViewSpeedAndLoad = new LineGraphView(getActivity(), "");
		graphViewSpeedAndLoad.setViewPort(0, 24);
		graphViewSpeedAndLoad.setGraphViewStyle(new GraphViewStyle(Color.BLACK,
				Color.BLACK, Color.LTGRAY));
		graphViewSpeedAndLoad.setScrollable(true);
		graphViewSpeedAndLoad.setShowLegend(true);
		graphViewSpeedAndLoad.setLegendAlign(LegendAlign.BOTTOM);

		// Create the different GraphViewSeries and the respective styles for
		// the second GraphView.
		GraphViewSeriesStyle speedSeriesStyle = new GraphViewSeriesStyle(
				AppUtils.SPEED_COLOR, 4);
		speedSeries = new GraphViewSeries(AppUtils.SPEED,
				speedSeriesStyle, new GraphViewData[0]);
		GraphViewSeriesStyle loadSeriesStyle = new GraphViewSeriesStyle(
				AppUtils.LOAD_COLOR, 4);
		loadSeries = new GraphViewSeries(AppUtils.LOAD,
				loadSeriesStyle, new GraphViewData[0]);

		// Add the initialized series to the GraphViews.
		graphViewHRAndResp.addSeries(heartRateSeries);
		graphViewHRAndResp.addSeries(respirationSeries);
		graphViewSpeedAndLoad.addSeries(speedSeries);
		graphViewSpeedAndLoad.addSeries(loadSeries);

		// The layout of the graph.
		LinearLayout layoutGraphHRResp = (LinearLayout) rootView
				.findViewById(R.id.graphHRResp);
		layoutGraphHRResp.addView(graphViewHRAndResp);
		LinearLayout layoutGraphSpeedLoad = (LinearLayout) rootView
				.findViewById(R.id.graphSpeedLoad);
		layoutGraphSpeedLoad.addView(graphViewSpeedAndLoad);

		// Layout of the numerical information panel.
		LinearLayout layoutNumericalInfo = (LinearLayout) rootView
				.findViewById(R.id.numericalinfo);
		heartRateTextView = (TextView) layoutNumericalInfo.findViewById(R.id.heartRateTV);
		heartRateTextView.setTextColor(AppUtils.HR_COLOR);
		respirationTextView = (TextView) layoutNumericalInfo.findViewById(R.id.respirationTV);
		respirationTextView.setTextColor(AppUtils.RESPIRATION_COLOR);
		speedTextView = (TextView) layoutNumericalInfo.findViewById(R.id.speedTV);
		speedTextView.setTextColor(AppUtils.SPEED_COLOR);
		loadTextView = (TextView) layoutNumericalInfo.findViewById(R.id.loadTV);
		loadTextView.setTextColor(AppUtils.LOAD_COLOR);
		
		return rootView;
	}

	@Override
	public void onStart() {
		super.onStart();

	}

	@Override
	public void onPause() {
		super.onPause();
	}

}
